/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * 
 * @author yaoyuming
 * 
 * @date 2020/8/31 0031
 */
package com.yao.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.yao.auth.handler.FailureHandler;
import com.yao.auth.handler.SuccessHandler;
import com.yao.auth.service.LoginService;

import lombok.extern.slf4j.Slf4j;

/**
 * spring security 配置类
 * 
 * @author yaoyuming
 * @date 2020/8/31 0031 14:28
 */
@Slf4j
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LoginService loginService;
    @Autowired
    private SuccessHandler successHandler;
    @Autowired
    private FailureHandler failureHandler;
    @Autowired
    private CustomSecurityProperties customSecurityProperties;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(loginService);
    }



/*    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }*/

    /**
     * 资源白名单
     */
    private String[] whitelist = {"/**/*.html", "/**/*.css", "/**/*.png", "/**/*.js", "/favicon.ico", "/*.html", "/login"};

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        formSecurity(http);
    }

    private void formSecurity(HttpSecurity http) throws Exception {
//        http.httpBasic().realmName("yao-0");

        http.formLogin().loginPage(customSecurityProperties.getLoginPage())
                .successHandler(successHandler)
                .failureHandler(failureHandler)
                .permitAll().and().logout().permitAll();

        csrf(http);

        header(http);

        // 白名单
        white(http);

        // 所有请求, 都需要认证
        http.authorizeRequests().anyRequest()
            // .permitAll();
            .authenticated();

    }

    /**
     * 配置请求头
     */
    private void header(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();
    }

    /**
     * 关闭csrf token
     */
    private void csrf(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }

    /**
     * 白名单
     */
    private void white(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(HttpMethod.GET, whitelist).permitAll();
    }

    /**
     * 装载BCrypt密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}